package me.zhengjie.modules.web.service.user.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import me.zhengjie.config.exception.WeChatException;
import me.zhengjie.constant.ConstantUtils;
import me.zhengjie.modules.system.domain.IdentityItem;
import me.zhengjie.modules.system.domain.WxUser;
import me.zhengjie.modules.web.domain.user.GetUserSuperior;
import me.zhengjie.modules.web.service.mybatisplus.IWxUserPlusService;
import me.zhengjie.modules.web.service.mybatisplus.IdentityItemPlusService;
import me.zhengjie.modules.web.service.user.IUserWebService;
import me.zhengjie.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
public class UserWebServiceImpl implements IUserWebService {

    @Autowired
    private IWxUserPlusService wxUserPlusService;

    @Autowired
    private IdentityItemPlusService identityItemPlusService;

    @Override
    public GetUserSuperior getUserSuperior(String openId) {
        if (StringUtils.isBlank(openId)){
            return null;
        }
        LambdaQueryWrapper<WxUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WxUser::getOpenId,openId);
        WxUser currentUser = wxUserPlusService.getOne(wrapper);
        if (Objects.isNull(currentUser)){
            throw new WeChatException("未查询到微信用户信息");
        }

        String superior = currentUser.getSuperior();
        if (StringUtils.isBlank(superior)){
            throw new WeChatException("无上级");
        }
        LambdaQueryWrapper<WxUser> oneWrapper = new LambdaQueryWrapper<>();
        oneWrapper.eq(WxUser::getOpenId,superior);
        WxUser oneUser = wxUserPlusService.getOne(oneWrapper);
        if (Objects.isNull(oneUser)){
            return null;
        }
        GetUserSuperior userSuperior = new GetUserSuperior();
        userSuperior.setOneLevelOpenId(superior);
        userSuperior.setOneInfo(oneUser);
        if (StringUtils.isNotBlank(oneUser.getIdentityNo())){
            LambdaQueryWrapper<IdentityItem> identityWrapper = new LambdaQueryWrapper<>();
            identityWrapper.eq(IdentityItem::getItemNo,oneUser.getIdentityNo()).eq(IdentityItem::getStatus, ConstantUtils.Valid);
            IdentityItem one = identityItemPlusService.getOne(identityWrapper);
            if (Objects.nonNull(one)){
                userSuperior.setOneLevelRatio(one.getOneLevelRatio());
            }
        }
        String oneSuperior = oneUser.getSuperior();
        if (StringUtils.isBlank(oneSuperior)){
            return userSuperior;
        }
        //查询二级
        LambdaQueryWrapper<WxUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WxUser::getOpenId, oneSuperior);
        WxUser twoUser = wxUserPlusService.getOne(queryWrapper);
        if (Objects.isNull(twoUser) || StringUtils.isBlank(twoUser.getSuperior())){
            return userSuperior;
        }
        userSuperior.setTwoUser(twoUser);
        userSuperior.setTwoLevelOpenId(oneSuperior);
        if (StringUtils.isNotBlank(twoUser.getIdentityNo())){
            LambdaQueryWrapper<IdentityItem> identityWrapper = new LambdaQueryWrapper<>();
            identityWrapper.eq(IdentityItem::getItemNo,twoUser.getIdentityNo()).eq(IdentityItem::getStatus, ConstantUtils.Valid);
            IdentityItem two = identityItemPlusService.getOne(identityWrapper);
            if (Objects.nonNull(two)){
                userSuperior.setTowLevelRatio(two.getTwoLevelRatio());
            }
        }
        return userSuperior;
    }
}
